/**
 * A StackPanel widget for the SensorDataViewer
 */
package org.hackystat.ui.sensordataviewer.client;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.StackPanel;

/**
 * A StackPanel widget for the SensorDataViewer.
 * @author David J. Nickles
 *
 */
public class ImageConsciousStackPanel extends StackPanel {

  /** {@literal} OPEN_BOOK journal open state. */        
  private static final String OPEN_BOOK = "images/open-book.gif"; 
  /** {@literal} CLOSED_BOOK journal closed state. */        
  private static final String CLOSED_BOOK = "images/closed-book.gif"; 
  private Image dataPanelImage;
  private String open;
  private String closed;
  private int lastIndexClicked;
  
  /**
   * Applies a Scriptaculous effect to an element.
   * @param element The element to reveal
   * @param effect The effect to apply
   * 
   * Usage: applyEffectMac(getWidget(3).getElement(), "BlindUp");
   */
  private native void applyEffectMac(Element element, String effect) /*-{
    var ne = $wnd._nativeExtensions;
    $wnd._nativeExtensions = false;
    $wnd.Effect[effect](element);
    $wnd._nativeExtensions = ne;
  }-*/; 
  
  /**
   * This method sets the references to the data panel's image.
   * @param img the image in the data panel
   * @param open the url of the image for an opened state
   * @param closed the url of the image for an closed state
   */
  public void setDataPanelImage(Image img, String open, String closed) {
    this.dataPanelImage = img;
    this.open = open;
    this.closed = closed;
  }

  /**
   * Shows the widget at the specified child index.
   * 
   * @param index the index of the child to be shown
   */
  public void showStack(int index) {
    super.showStack(index);
    if (this.lastIndexClicked == 3) {
      if (index == 3) {
        this.getWidget(3).setVisible(!this.getWidget(3).isVisible());
        if (this.getWidget(3).isVisible()) {
          this.dataPanelImage.setUrl(OPEN_BOOK);
        } 
        else {
          this.dataPanelImage.setUrl(CLOSED_BOOK);
        }
      }
    }
    else {
      if (this.dataPanelImage != null) {
        if (index == 3) {
          this.dataPanelImage.setUrl(OPEN_BOOK);
        } 
        else {
          this.dataPanelImage.setUrl(CLOSED_BOOK);
        }
      }
    }
    this.lastIndexClicked = index;
  }
}


